import App from "./App";
import messages from "./locale/index";

let i18nConfig = {
  locale: uni.getLocale(), // 获取已设置的语言
  messages,
};

import Vue from "vue";
import { file, business, utilities } from "@/utils/common.js";
import * as filters from "./filters"; // global filters 全局过滤
import uView from "@/uni_modules/uview-ui";
import VueI18n from "vue-i18n";

Vue.use(uView);
Vue.use(VueI18n);

Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]);
});

const i18n = new VueI18n(i18nConfig);
Vue.config.productionTip = false;
App.mpType = "app";

const app = new Vue({
  i18n,
  ...App,
});
app.$mount();

/**
 * 返回图片
 * @param {Object} picture
 * @param {Object} type
 */
Vue.prototype.$getImg = function (picture, type) {
  if (!picture) {
    if (type == "pic") {
      return "/static/images/empty_pic.png";
    } else if (type == "head") {
      return "/static/images/empty_head.jpg";
    }
  }
  return file.getImg(picture);
};

/**
 * 业务操作日志
 * @param {Object} operateType 操作类型--add:新增，edit:修改，query:列表，detail:详情，import:导入，export:导出
 * @param {Object} businessKey 业务Key
 * @param {Object} operateKey  操作关键标识(业务主键)
 */
Vue.prototype.$saveOpLog = function (operateType, businessKey, operateKey) {
  return business.saveOpLog(operateType, businessKey, operateKey);
};

// 页面跳转拦截(页面跳转必须使用绝对路径)
uni.addInterceptor("navigateTo", {
  invoke(e) {
    // 去除参数
    let pageUrl = e.url.split("?")[0];
    // 只拦截本应用内页面，webview页面不做拦截
    if (pageUrl.indexOf("/pages") !== -1) {
      if (ROUTES.findIndex((r) => r.path == pageUrl) === -1) {
        uni.navigateTo({
          url: "/pages/base/none-page",
        });
        return false;
      }
    }
    return true;
  },
  success(e) {
    // console.log(e)
  },
});

// 页面tabbar拦截(校验版本)
uni.addInterceptor("switchTab", {
  invoke(e) {
    let currentVersion = uni.getStorageSync("currentVersion");
    let latestVersion = uni.getStorageSync("latestVersion");
    let pageUrl = e.url.split("?")[0];
    if (pageUrl.indexOf("/home/index") === -1) {
      if (
        utilities.compareVersion(currentVersion, latestVersion.appVersion) ==
          -1 &&
        latestVersion.versionAttr === 1
      ) {
        return false;
      }
    }
    return true;
  },
  success(e) {
    // console.log(e)
  },
});
